package com.rockxin.cat.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.rockxin.cat.MainActivity;
import com.rockxin.cat.R;
import com.rockxin.cat.base.BaseActivity;
import com.rockxin.cat.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <b>类名称：    GuidePageActivity             <br/>
 * <b>类描述：                        <br/>
 * <b>创建人：    Lans               <br/>
 * <b>创建时间：  2018/3/1 11:22     <br/>
 * <b>修改人：                        <br/>
 * <b>修改时间：                      <br/>
 * <b>修改备注：                      <br/>
 *
 * @version 1.0.0 <br/>
 */
public class GuidePageActivity extends BaseActivity {
    private static final String TAG = "GuidePageActivity";
    @BindView(R.id.jumpPage)
    TextView jumpPage;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private GuidePageActivity mGuidePageActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mGuidePageActivity = this;

    }

    @Override
    protected int setLayout() {
        return R.layout.activity_guide_page;
    }

    @Override
    public void initData() {
        SPUtils spUtils = new SPUtils("Guide");
        spUtils.put("first", false);

        ImageView imageView = new ImageView(GuidePageActivity.this);
        imageView.setImageResource(R.mipmap.page1);
        ImageView imageView2 = new ImageView(GuidePageActivity.this);
        imageView2.setImageResource(R.mipmap.page2);
        ImageView imageView3 = new ImageView(GuidePageActivity.this);
        imageView3.setImageResource(R.mipmap.page3);
        ImageView imageView4 = new ImageView(GuidePageActivity.this);
        imageView4.setImageResource(R.mipmap.page4);
        final List<View> viewList = new ArrayList<>();
        viewList.add(imageView);
        viewList.add(imageView2);
        viewList.add(imageView3);
        viewList.add(imageView4);
        GuidePageAdapter guidePageAdapter = new GuidePageAdapter(viewList);
        viewPager.setAdapter(guidePageAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                                              @Override
                                              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                                                  if (position == 3) {
                                                      startActivity(new Intent(mGuidePageActivity, MainActivity.class));
                                                      finish();
                                                  }
                                              }

                                              @Override
                                              public void onPageSelected(int position) {

                                              }

                                              @Override
                                              public void onPageScrollStateChanged(int state) {

                                              }
                                          }
        );
    }

    @OnClick(R.id.jumpPage)
    public void onViewClicked() {
        startActivity(new Intent(mGuidePageActivity, MainActivity.class));
        finish();
    }

    static class GuidePageAdapter extends PagerAdapter {
        List<View> viewList;

        public GuidePageAdapter(List<View> viewList) {
            this.viewList = viewList;
        }

        @Override
        public int getCount() {
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
